package com.multithreading.chart1.createMulitithreading;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @program: java-multithreading
 * @description: 继承thread类方式创建
 * @author: 陈庆彪
 * @create: 2020-06-20 09:30
 **/
public class TheadTest {
    //继承Thread类并重写run方法
    public static class MyThread extends Thread{
        @Override
        public void run() {
            System.out.println("我是一个子线程！");
        }

    }

    public static class RunableTask implements Runnable{
        @Override
        public void run() {
            System.out.println("我也是一个子线程！");
        }
    }

    public static class CallerTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            return "hello";
        }

    }
    public static void main(String[] args) {
        //创建线程
        MyThread thread=new MyThread();
        //启动线程
        thread.start();
        //runAble
        RunAableTest.RunableTask task=new RunAableTest.RunableTask();
        new Thread(task).start();
        new Thread(task).start();

        //futrue方式
        //创建异步任务
        FutureTask<String> futureTask=new FutureTask<>(new FutureTest.CallerTask());

        //启动线程
        new Thread(futureTask).start();

        //等待任务执行完毕，并返回结果
        try {
            String result=futureTask.get();
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



}
